import time

# 1.计算阶乘
def factorial(num):
    if num == 0: # 0!=1
        return 1
    return num*factorial(num-1)

print(factorial(6))

# 2.反转列表
def resvsal(lst:list) -> list:
    if not lst:
        return []
    a = [lst.pop()]
    a.extend(resvsal(lst))
    return a

print(resvsal([i*i for i in range(10)]))


# 5.计算斐波那契数列，比较比较性能
class TestFib(object):
    def getbyrecursion(num):
        def fib(tmp,num):
            if num<=2:
                return tmp[num-1]
            res = [tmp[1],sum(tmp)]
            num-=1
            return fib(res,num)
        return fib([1,1],num)
    
    def getbyfor(num):
        tmp=[1,1]
        if num <= 2:
            return tmp[num-1]
        for _ in range(num-2):
            tmp=[tmp[1],sum(tmp)]
        return tmp[1]
    
    def testspeed(num):
        t1=time.time()
        for i in range(num):
            TestFib.getbyrecursion(i+1)
        t2=time.time()
        print(f"recursion cost {t2-t1} s")
        t1=time.time()
        for i in range(num):
            TestFib.getbyfor(i+1)
        t2=time.time()
        print(f"for loop cost {t2-t1} s")

print(TestFib.getbyrecursion(10))
print(TestFib.getbyfor(10))
TestFib.testspeed(900)